﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using OBUCare.Web.Models;
using OBUCare.Core.Common;
using System.IO;
using OBUCare.Core.BL;
using OBUCare.Core.Models;
using OBUCare.Core.DataAccess;
using OBUCare.Web.Infrastructure;
using System.Web.Http;
using System.Net.Http;
using System.Net;
using System.Text;


namespace OBUCare.Web.Controllers
{
    public class ImportController : Controller
    {
        OBUCareOrderMgr obuMgr = new OBUCareOrderMgr();
        ConfigMgr configMgr = new ConfigMgr();

        //
        // GET: /Import/

        public ActionResult Index()
        {
            return View();
        }


        /// <summary>
        /// 动态加载Excel预览控件
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult ExcelPreviewGrid(string id)
        {
            try
            {
                var infos = this.Session["infos_" + id] as List<ImportExcelDataViewModel>;
                var nameList = this.Session["nameList_" + id] as List<string>;

                ViewBag.FaultNameList = nameList;

                return PartialView("_ExcelPreviewGrid", infos);
            }
            catch (Exception ex)
            {

                TraceHelper.TraceError("预览导入的Excel出错：",ex);

                Response.StatusCode = 500;
                //Response.Status = "error";
                Response.StatusDescription = ex.Message;

                Response.AppendHeader("message", ex.Message);
                return Content(ex.Message, "text/plain");
                //throw ex;
                return Json(new { success = false, message = ex.Message }, JsonRequestBehavior.AllowGet);
                //var resp = new HttpResponseMessage(HttpStatusCode.InternalServerError)
                //{
                //    Content = new StringContent(string.Format("No product with ID = {0}", id)),
                //    ReasonPhrase = "Product ID Not Found"
                //};
                //throw new HttpResponseException(resp);              
            }
        }

        /// <summary>
        /// 检查上传的excel数据是否正确
        /// </summary>
        /// <param name="strIDs"></param>
        /// <returns></returns>
        public ActionResult CheckExcelPreviewGrid(string id)
        {
            bool success = true;
            string message = "";
            try
            {
                if (!".xls".Equals(Path.GetExtension(id), StringComparison.InvariantCultureIgnoreCase))
                    throw new Exception("仅支持excel 2003格式，请转换成2003格式");

                var physicalPath = UploadController.GetFilePathInTempFolder(id);
                NPOIExcelHelper excel = new NPOIExcelHelper(physicalPath);
                List<string> faultNameList = getFaultNameList(excel);
                var infos = readExcelContent(excel);
                ViewBag.FaultNameList = faultNameList;

                //读完Excel后，将结果同时也保存到Session中，以便于导入
                SaveExcelContentToSession(id, infos, faultNameList);
            }
            catch (Exception ex)
            {
                success = false;
                message = ex.Message;

                TraceHelper.TraceError("验证待导入的数据是否正确：",ex);
            }

            return Json(new { success = success, message = message });
        }


        private void SaveExcelContentToSession(string fileName, List<ImportExcelDataViewModel> infos, List<string> nameList)
        {
            this.Session.Add("infos_" + fileName, infos);
            this.Session.Add("nameList_" + fileName, nameList);
        }

        private bool importToDB(string fileName)
        {
            var u = UIUtility.GetCurrentUser();
            var infos = this.Session["infos_" + fileName] as List<ImportExcelDataViewModel>;
            var nameList = this.Session["nameList_" + fileName] as List<string>;

            var dict = getFaultNamePartTypeMapping(nameList);

            IList<REPAIR_ORDER_PRICE> priceList = new List<REPAIR_ORDER_PRICE>();

            infos.Each(info =>
            {
                priceList.CustomizeAddRange(ToRepairOrderPrice(info, u, nameList, dict));
            });

            return obuMgr.ImportRepairOrderPrice(priceList, u);
        }

        //private bool CheckSameSuppliers(List<ImportExcelDataViewModel> list) 
        //{
        //    bool success = false;
        //    string message = "请选同一厂商的的标签";

        //    var strObuNos = list.ConcatToString(i => string.Format("'{0}'", i.OBU_NO.Trim()), ",");



        //    var Items = obuMgr.GetRepairOrders(string.Format(" OBU_NO in ({0}) ", strObuNos));

        //    var result = (from i in Items
        //                 select i.SUPPLIERS_ID).ToList();

        //    if (result.Distinct().Count() == 1)
        //    {
        //        message = "";
        //        success = true;
        //    }
        //    if (success)
        //        return success;
        //    else
        //        throw new Exception(message);
        //}



        private List<ImportExcelDataViewModel> readExcelContent(NPOIExcelHelper excel)
        {

            //返回时间
            DateTime? returnDate = null;
            if (excel[ConfigMgr.ReturnDateIndex.Key, ConfigMgr.ReturnDateIndex.Value].IsNullOrEmpty())
                returnDate = System.DateTime.Now;
            else
            {
                string strDt = excel[ConfigMgr.ReturnDateIndex.Key, ConfigMgr.ReturnDateIndex.Value];
                var tempDate = strDt.ToDateTime();
                if (tempDate.HasValue)
                {
                    returnDate = tempDate.Value;
                }
                else
                {
                    throw new ArgumentException(string.Format("返回日期: {0} 的格式不正确,请修改时期时间为年月日用'-'分割，如：2013-3-12", strDt));
                }
            }

            //厂商
            var supplier = excel[ConfigMgr.SupplierIndex.Key, ConfigMgr.SupplierIndex.Value];


            List<ImportExcelDataViewModel> list = new List<ImportExcelDataViewModel>();

            for (int i = ConfigMgr.startRowIndex; i <= excel.LastRowIndex; i++)
            {
                ImportExcelDataViewModel temp = new ImportExcelDataViewModel();
                string repairNo = excel[i, ConfigMgr.repairNoIndex];

                if (repairNo.IsNullOrEmpty())
                    continue;


                string obuNo = excel[i, ConfigMgr.OBUIndex];
                string dispose_Type = excel[i, ConfigMgr.DISPOSE_TYPEIndex].Trim(); //厂商处理意见 



                var dispose_Types = new List<string> { "0", "1", "2" };
                if (!dispose_Types.Contains(dispose_Type))
                    throw new ArgumentException(string.Format("厂商处理意见: {0} 的格式不正确,厂商处理意见仅能为0,1,2", dispose_Type));



                string reMark = excel[i, ConfigMgr.reMarkIndex];

                if (!repairNo.IsNullOrEmpty())
                {
                    list.Add(temp);
                    temp.Index = list.Count();

                    temp.ReturnDate = returnDate;
                    temp.Supplier = supplier;


                    temp.RepairNo = repairNo;
                    temp.OBU_NO = obuNo;
                    temp.DISPOSE_TYPE = dispose_Type;
                    temp.remark = reMark;

                    setFaults(excel, i, temp);

                }
            }


            return list;
        }

        private List<string> getFaultNameList(NPOIExcelHelper excel)
        {
            List<string> nameList = new List<string>();

            for (int i = ConfigMgr.startFaultIndex; i <= ConfigMgr.endFaultIndex; i++)
            {
                var temp = excel[ConfigMgr.faultNameRowIndex, i];
                nameList.Add(temp);
            }

            return nameList;
        }

        /// <summary>
        /// 获取标题与 配件类型元数据的对应关系
        /// </summary>
        /// <param name="nameList"></param>
        /// <returns></returns>
        private Dictionary<string, PARTS_TYPE> getFaultNamePartTypeMapping(List<string> nameList)
        {
            Dictionary<string, PARTS_TYPE> dict = new Dictionary<string, PARTS_TYPE>();
            var partTypes = configMgr.GetPartsType().ToList();
            nameList.Each(n =>
            {
                var part = partTypes.FirstOrDefault(p => p.PARTS_NAME.Equals(n));
                dict.Add(n, part);
            });

            return dict;
        }

        /// <summary>
        /// 读取Excel中指定行的故障点部分，同时将值写入到Model中
        /// </summary>
        /// <param name="excel"></param>
        /// <param name="rowIndex"></param>
        /// <param name="model"></param>
        private void setFaults(NPOIExcelHelper excel, int rowIndex, ImportExcelDataViewModel model)
        {
            int faultIndex = 1;
            for (int i = ConfigMgr.startFaultIndex; i <= ConfigMgr.endFaultIndex; i++)
            {

                var temp = excel[rowIndex, i].CustomizeTrim();

                //如果是其它列
                if (i == ConfigMgr.OtherPartIndex)
                {
                    if (!temp.IsNullOrEmpty())
                    {
                        if (!temp.Todecimal().HasValue)
                        {
                            throw new ArgumentException(string.Format("故障点: 其它 的格式不正确,其它故障点仅能为金额", temp));
                        }
                    }
                }
                else //非其它列
                {
                    var availableValues = new List<string> { "0", "Y", "N", "y", "n", "" };
                    if (!availableValues.Contains(temp))
                        throw new ArgumentException(string.Format("故障点: {0} 的格式不正确,故障点仅能为Y,N,0", temp));
                }

                model[faultIndex++] = temp;
            }


        }

        /// <summary>
        /// 将一个ImportExcelDataViewModel转成相应的一系列报价信息
        /// </summary>
        /// <returns></returns>
        private List<REPAIR_ORDER_PRICE> ToRepairOrderPrice(ImportExcelDataViewModel m, OPERATOR u, List<string> nameList, Dictionary<string, PARTS_TYPE> dict)
        {
            List<REPAIR_ORDER_PRICE> list = new List<REPAIR_ORDER_PRICE>();

            var supplier = configMgr.GetSupplierByName(m.Supplier);
            if (supplier == null)
                throw new ApplicationException(string.Format("不存{0}对应的厂商，请确认厂商名称后重试", m.Supplier));

            var repairOrder = obuMgr.GetRepairOrder(m.RepairNo);
            if (repairOrder == null)
            {
                throw new ApplicationException(string.Format("不存{0}对应的维修单号，请确认维修单后重试", m.RepairNo));
            }

            int faultIndex = 1;//model中Fault1(故障点)属性对应的数据后缀
            for (int i = ConfigMgr.startFaultIndex; i <= ConfigMgr.endFaultIndex; i++, faultIndex++)
            {
                REPAIR_ORDER_PRICE p = new REPAIR_ORDER_PRICE();

                string priceFlag = m[faultIndex];
                if (/*(!ConfigMgr.PriceFlag_Invalid.Equals(priceFlag, StringComparison.InvariantCultureIgnoreCase)) && */
                    (!priceFlag.IsNullOrEmpty()))
                    //p.SUPPLIER_PRICE = supplierPrice;
                    p.PRICE_FLAG = priceFlag;
                else
                    continue;

                p.setDisposeType((DisposeType)m.DISPOSE_TYPE.ToInt().Value);

                p.CREATED_BY = u.OPERATOR_NO;
                p.CREATED_DATE = DateTime.Now;
                p.MODIFY_BY = u.OPERATOR_NO;
                p.MODIFY_DATE = DateTime.Now;

                p.SUPPLIER_ID = supplier.ID;

                p.REPAIR_ORDER_ID = repairOrder.ID;
                p.OBU_NO = repairOrder.OBU_NO;



                p.BACK_DATE = m.ReturnDate.Value;
                p.REMARK = m.remark;

                string faultName = nameList.GetValueTyIndex(faultIndex - 1);

                PARTS_TYPE ptype = dict.GetValue(faultName);

                if (ptype == null)
                {
                    throw new ApplicationException(string.Format("系统配置表不存在故障点:{0}", faultName));
                }

                p.FAULT_PARTS = ptype.ID;
                //其它故障点时
                if (ptype.PARTS_NAME == ConfigMgr.OtherPartName)
                {
                    if (repairOrder.REPAIR_TYPE != 2)
                    {
                        var price = priceFlag.Todecimal();
                        if (!price.HasValue)
                            price = 0;

                        p.SUPPLIER_PRICE = price.Value; //厂商报价
                        if (p.SUPPLIER_PRICE > 0)
                        {
                            p.PRICE = price.Value + ConfigMgr.OtherPartCost; //速通报价    
                        }
                        else
                            p.PRICE = p.SUPPLIER_PRICE; //速通报价 
                    }
                    else
                    {
                        p.SUPPLIER_PRICE = 0;
                        p.PRICE = 0;
                    }

                }
                else
                {
                    if (repairOrder.REPAIR_TYPE != 2)
                    {
                        p.PRICE = ptype.PRICE; //速通报价    
                        p.SUPPLIER_PRICE = ptype.SUPPLIER_PRICE; //厂商报价
                    }
                    else
                    {
                        p.SUPPLIER_PRICE = 0;
                        p.PRICE = 0;
                    }
                }
                list.Add(p);
            }

            return list;
        }

        /// <summary>
        /// 导入前进行数据校验
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult CheckBeforImport(string id)
        {
            bool success = true;
            string message = "";
            bool hasImportBefore = false; //是否已经导入过数据 
            try
            {
                var infos = this.Session["infos_" + id] as List<ImportExcelDataViewModel>;

                if (infos!=null && infos.Count()==0)
                {
                    message = string.Format("导入数据不能为空，请确保数据完整再导入");
                    throw new Exception(message);
                }

                var nameList = this.Session["nameList_" + id] as List<string>;

                var strNos = infos.ConcatToString(i => string.Format("'{0}'", i.RepairNo.Trim()), ",");
                var Items = obuMgr.GetRepairOrders(string.Format(" REPAIR_ORDER_NO in ({0}) ", strNos)).ToList();


                List<string> ErrorReportOrders = new List<string>();
                //判断维修单是否存在
                infos.Each(info =>
                    {
                        if (!Items.Contains(i => i.REPAIR_ORDER_NO.Equals(info.RepairNo)))
                            ErrorReportOrders.Add(info.RepairNo);
                    });

                if (ErrorReportOrders.Count > 0)
                {
                    message = string.Format("不存在维修单号：{0}，请修改后重新导入", ErrorReportOrders.ConcatToString("、"));
                    throw new Exception(message);
                }

                //判断维修单与标签号是否一致
                infos.Each(info =>
                {
                    var item = Items.FirstOrDefault(i => i.REPAIR_ORDER_NO.Equals(info.RepairNo));
                    if (item != null)
                    {
                        if (!item.OBU_NO.Equals(info.OBU_NO))
                            ErrorReportOrders.Add(info.RepairNo);
                    }
                    else
                        throw new Exception(string.Format("不存在维修单号：{0}，请修改后重新导入", item.REPAIR_ORDER_NO));
                });

                if (ErrorReportOrders.Count > 0)
                {
                    message = string.Format("维修单号：{0}，对应的标签号不一致，请修改后重新导入", ErrorReportOrders.ConcatToString("、"));
                    throw new Exception(message);
                }

                //是否导入同一厂商数据
                var itemsGroupbySuppler = (from i in Items
                                           group i by i.SUPPLIERS_ID into g
                                           orderby g.Count() descending
                                           select g).ToList();

                string supplierName = null;
                if (itemsGroupbySuppler.Count > 1)
                {
                    supplierName = itemsGroupbySuppler[0].First().SUPPLIER_ShortName;
                    for (int i = 1; i < itemsGroupbySuppler.Count; i++)
                    {
                        itemsGroupbySuppler[i].Each(item => ErrorReportOrders.Add(item.REPAIR_ORDER_NO));
                    }
                }
                if (ErrorReportOrders.Count > 0)
                {
                    message = string.Format("维修单号：{0}，不属于{1}，请修改后重新导入", ErrorReportOrders.ConcatToString("、"), supplierName);
                    throw new Exception(message);
                }

                //是否有导入过的维修单
                Items.Each(i =>
                    {
                        if (obuMgr.ImportBefore(i.ID))
                            ErrorReportOrders.Add(i.REPAIR_ORDER_NO);
                    });

                if (ErrorReportOrders.Count > 0)
                {
                    message = string.Format("维修单号：{0}，已经导入过，是否覆盖？", ErrorReportOrders.ConcatToString("、"));
                    hasImportBefore = true;
                    throw new Exception(message);
                }

                success = true;
            }
            catch (Exception ex)
            {
                success = false;
                message = ex.Message;
            }

            return Json(new { success = success, message = message, hasImportBefore = hasImportBefore });
        }



        [System.Web.Mvc.HttpPost]
        public ActionResult ConfirmImport(string id)
        {
            bool success = false;
            string message = null;

            try
            {
                success = importToDB(id);
            }
            catch (Exception ex)
            {
                success = false;
                message = ex.Message;
            }

            return Json(new { success = success, message = message });
        }

        /// <summary>
        /// 导出厂商反馈数据
        /// </summary>
        /// <param name="strIDs"></param>
        /// <returns></returns>
        public ActionResult Export(string strIDs)
        {
            string errMsg = "请选择要导出的维修单";
            if (strIDs.IsNullOrEmpty())
                return Content(errMsg);
            string exportFileTitle = "返厂信息.xls";

            ActionResult result = null;
            try
            {
                var template = UIUtility.GetTemplateFilePath("ObuExportImport.xls");
                var export = new ExportHelper(template);
                List<string> nameList = null; //保存Excel中故障点列表

                var Items = obuMgr.GetRepairOrders(string.Format(" ID in ({0}) ", strIDs)).ToList();
                if (Items.Count() < 0)
                    return Content(errMsg);

                var FirstItem = Items.First();

                exportFileTitle = string.Format("返厂信息({0}).xls", FirstItem.SUPPLIER_ShortName); //设置导出文件名

                int sheetIndex = 0;
                int startRowIndex = ConfigMgr.startRowIndex;
                int index = 1; //序号

                #region 获取所有导入的厂商报价

                var allPrices = obuMgr.GetRepairOrderPrices(string.Format(" REPAIR_ORDER_ID in ({0}) ", strIDs)).ToList();

                
                #endregion

                //导出厂商和日期
                export.ExportAddionalInfomation((excel, ep) =>
                {
                    excel.ActiveSheetIndex = sheetIndex;

                    nameList = getFaultNameList(excel); //读取故障点列表

                    excel[ConfigMgr.SupplierIndex.Key, ConfigMgr.SupplierIndex.Value] = FirstItem.SUPPLIER_ShortName;   //写入厂家
                    excel[ConfigMgr.ReturnDateIndex.Key, ConfigMgr.ReturnDateIndex.Value] = DateTime.Now.ToString(UIConstants.DateFormatString); //写入日期
                    //if (Items.Count() > 1)
                    //{
                        excel.ShiftRows(startRowIndex + 1, Items.Count()+startRowIndex, Items.Count());
                    //}
                });

                var dictFaultNamePartType = getFaultNamePartTypeMapping(nameList); //excel中的故障点名与数据库中的故障点配置映射

                //导出主要数据
                export.ExportToExcel(Items, (item, excel, rowIndex) =>
                {
                    int colIndex = 0;

                    excel.SetCellValue(rowIndex, colIndex++, index++); //序号
                    excel[rowIndex, colIndex++] = item.REPAIR_ORDER_NO; //维修单号
                    excel[rowIndex, colIndex++] = item.OBU_NO; //标签号

                    var disposeType = configMgr.GetTypeValue(ConfigMgr.tblDisposeType, item.DISPOSE_TYPE.ToText());
                    //厂商处理意见
                    excel[rowIndex, ConfigMgr.DISPOSE_TYPEIndex] = disposeType.DICT_TITLE;

                    //客户确认是否维修
                    ConfirmRepairedStatus repari = (ConfirmRepairedStatus)item.CONFIRM_REPAIRED;
                    string repairText = "";
                    if (repari == ConfirmRepairedStatus.Repair)
                    {
                        repairText = "是";
                    }
                    else if (repari == ConfirmRepairedStatus.NotRepair)
                    {
                        repairText = "否";

                        var tracking = (from i in new OBU_CAREContext().REPAIR_ORDER_TRACKING.ToList()
                                        where i.REPAIR_ORDER_Status == RepairOrderStatus.ConnectionWithoutCustomers.ToInt()
                                        && i.REPAIR_ORDER_ID == item.ID
                                        select i).FirstOrDefault();
                        if (tracking != null)
                        {
                            excel[rowIndex, ConfigMgr.DISPOSE_TYPEIndex + 2] = tracking.REMARK;
                        }
                    }
                    excel[rowIndex, ConfigMgr.IsRepaired] = repairText;


                    #region 处理厂商导入的报价


                    colIndex = ConfigMgr.startFaultIndex;

                    nameList.Each(n =>
                        {
                            var faultType = dictFaultNamePartType.GetValue(n);
                            if (faultType != null)
                            {
                                var price = (from p in allPrices
                                             where (p.REPAIR_ORDER_ID == item.ID) && (p.FAULT_PARTS == faultType.ID)
                                             orderby p.CREATED_DATE
                                             select p).FirstOrDefault();

                                if (price != null)
                                {
                                    excel[rowIndex, colIndex] = price.PRICE_FLAG; //故障标识
                                    if (excel[rowIndex, ConfigMgr.reMarkIndex] == "")
                                        excel[rowIndex, ConfigMgr.reMarkIndex] = price.REMARK; //写入备注                                    
                                }

                            }
                            colIndex++;
                        });
                 
                   
                    #endregion


                    //设置样式
                    for (int c = 0; c <= 21; c++)
                    {
                        excel.SetCellStyle(startRowIndex, c, rowIndex, c);
                    }

                }, sheetIndex, startRowIndex);

                result = export.DownLoad(this.File, exportFileTitle);
            }
            catch (Exception ex)
            {
                result = ExportHelper.HandleExportException(this, ex, exportFileTitle);
            }
            return result;
        }

        /// <summary>
        /// 检查厂商是否一致
        /// </summary>
        /// <param name="strIDs"></param>
        /// <returns></returns>
        public ActionResult CheckSameSuppliers(string strIDs)
        {
            bool success = false;
            string message = "请选同一厂商的的标签";
            var Items = obuMgr.GetRepairOrders(string.Format(" ID in ({0}) ", strIDs));

            var result = (from i in Items
                          select i.SUPPLIERS_ID).ToList();

            if (result.Distinct().Count() == 1)
            {
                message = "";
                success = true;
            }
            return Json(new { success = success, message = message });
        }



        /// <summary>
        /// 导出维修单列表
        /// </summary>
        /// <param name="strIDs"></param>
        /// <returns></returns>
        public ActionResult ExportOrderList(string strIDs)
        {
            //导出前，异步删除昨天生成的临时文件
            UIUtility.RemoveFilesinTempFolder();

            string errMsg = "请选择要导出的维修单";
            if (strIDs.IsNullOrEmpty())
                return Content(errMsg);
            string exportFileTitle = "维修单列表.xls";
            ActionResult result = null;
            try
            {
                var template = UIUtility.GetTemplateFilePath("维修单列表.xls");
                var export = new ExportHelper(template);



                var Items = obuMgr.GetRepairOrders(string.Format(" ID in ({0}) ", strIDs)).ToList();
                if (Items.Count() < 0)
                    return Content(errMsg);



                int sheetIndex = 0;
                int headRowIndex = 1;
                int startRowIndex = 2;
                int index = 1; //序号



                //导出表头
                /*export.ExportAddionalInfomation((excel, ep) =>
                {
                    excel.ActiveSheetIndex = sheetIndex;

                    int colIndex = 0;

                    excel[headRowIndex, colIndex++] = "序号";  //序号
                    excel[headRowIndex, colIndex++] = "维修单号";
                    excel[headRowIndex, colIndex++] = "标签号";
                    excel[headRowIndex, colIndex++] = "客户姓名";
                    excel[headRowIndex, colIndex++] = "手机号";
                    excel[headRowIndex, colIndex++] = "是否维修";
                    excel[headRowIndex, colIndex++] = "备签号";
                    excel[headRowIndex, colIndex++] = "厂商";
                    excel[headRowIndex, colIndex++] = "型号";
                    excel[headRowIndex, colIndex++] = "受理日期";
                    excel[headRowIndex, colIndex++] = "录入日期";
                    excel[headRowIndex, colIndex++] = "受理营业厅";
                    excel[headRowIndex, colIndex++] = "维修单状态";

                    //设置样式
                    for (int c = 0; c < colIndex; c++)
                    {
                        excel.SetCellStyle(headRowIndex, 0, headRowIndex, c);
                    }
                    
                });*/



                //导出主要数据
                export.ExportToExcel(Items, (item, excel, rowIndex) =>
                {
                    int colIndex = 0;

                    excel.SetCellValue(rowIndex, colIndex++, index++); //序号
                    excel[rowIndex, colIndex++] = item.REPAIR_ORDER_NO; //维修单号
                    excel[rowIndex, colIndex++] = item.OBU_NO; //标签号                    

                    excel[rowIndex, colIndex++] = item.USER_NAME;
                    excel[rowIndex, colIndex++] = item.MOBILE_TEL_NO;
                    excel[rowIndex, colIndex++] = item.CONFIRM_REPAIRED_Title;
                    excel[rowIndex, colIndex++] = item.SPARE_OBU_NO;
                    excel[rowIndex, colIndex++] = item.SUPPLIER_ShortName;
                    excel[rowIndex, colIndex++] = item.SPEC_NAME;
                    excel[rowIndex, colIndex++] = item.REPORT_DATE.ToString(UIConstants.DateFormatString);
                    excel[rowIndex, colIndex++] = item.CREATED_DATE.ToString(UIConstants.DateFormatString);
                    excel[rowIndex, colIndex++] = item.AGENT_NAME;
                    excel[rowIndex, colIndex++] = item.StatusTitle;


                    //设置样式
                    for (int c = 0; c < colIndex; c++)
                    {
                        excel.SetCellStyle(startRowIndex, c, rowIndex, c);
                        //excel.SetCellStyle(startRowIndex, 1, rowIndex, c);
                    }

                }, sheetIndex, startRowIndex);

                result = export.DownLoad(this.File, exportFileTitle);
            }
            catch (Exception ex)
            {
                result = ExportHelper.HandleExportException(this, ex, exportFileTitle);
            }
            return result;
        }

    }
}
